home *** CD-ROM | disk | FTP | other *** search
- unit DrBobCGI;
- {$I-}
- interface
- type
- TCGIprotocol = (CGI,WinCGI);
- var
- CGIProtocol: TCGIprotocol = CGI;
-
- type
- TRequestMethod = (Unknown,Get,Post);
- var
- RequestMethod: TRequestMethod = Unknown;
-
- var
- ContentLength: Integer = 0;
- Data: AnsiString = '';
-
- function Value(const Field: ShortString): ShortString;
-
- function ValueAsInteger(const Field: ShortString): Integer;
-
- implementation
- uses
- Windows, SysUtils;
-
- function Value(const Field: ShortString): ShortString;
- var
- i: Integer;
- len: Byte absolute Result;
- begin
- Len := 0;
- i := Pos('&'+Field+'=',Data);
- if i = 0 then
- begin
- i := Pos(Field+'=',Data);
- if i > 1 then i := 0
- end
- else Inc(i); { skip '&' }
- if i > 0 then
- begin
- Inc(i,Length(Field)+1);
- while Data[i] <> '&' do
- begin
- Inc(Len);
- Result[Len] := Data[i];
- Inc(i)
- end
- end
- end {Value};
-
- function ValueAsInteger(const Field: ShortString): Integer;
- begin
- try
- Result := StrToInt(Value(Field))
- except
- Result := 0
- end
- end {ValueAsInteger};
-
- var
- P: PChar;
- i: Integer;
- Str: ShortString;
-
- initialization
- P := GetEnvironmentStrings;
- while P^ <> #0 do
- begin
- Str := StrPas(P);
- if Pos('REQUEST_METHOD=',Str) > 0 then
- begin
- Delete(Str,1,Pos('=',Str));
- if Str = 'POST' then RequestMethod := Post
- else
- if Str = 'GET' then RequestMethod := Get
- end;
- if Pos('CONTENT_LENGTH=',Str) = 1 then
- begin
- Delete(Str,1,Pos('=',Str));
- ContentLength := StrToInt(Str)
- end;
- if Pos('QUERY_STRING=',Str) > 0 then
- begin
- Delete(Str,1,Pos('=',Str));
- SetLength(Data,Length(Str)+1);
- Data := Str
- end;
- Inc(P, StrLen(P)+1)
- end;
- if RequestMethod = Post then
- begin
- SetLength(Data,ContentLength+1);
- for i:=1 to ContentLength do read(Data[i]);
- Data[ContentLength+1] := '&';
- { if IOResult <> 0 then { skip }
- end;
- i := 0;
- while i < Length(Data) do
- begin
- Inc(i);
- if Data[i] = '+' then Data[i] := ' ';
- if Data[i] = '%' then { special code }
- begin
- Str := '$00';
- Str[2] := Data[i+1];
- Str[3] := Data[i+2];
- Delete(Data,i+1,2);
- Data[i] := Chr(StrToInt(Str))
- end
- end;
- if i > 0 then Data[i+1] := '&'
- else Data := '&';
- finalization
- Data := ''
- end.
-